package com.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RequestWrapper;
import java.io.IOException;
import java.io.PrintWriter;

/** 
* @Description: servlet接口练习
* @Param:  
* @return:  
* @Author: lizexin 
* @Date: 2020/11/27 
*/
public class HelloServlet extends HttpServlet {
    @Override
    //doGet处理HTTP GET请求
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter(); //输出字符流数据
        try {
            request.setCharacterEncoding("UTF-8");//设置对客户端请求和数据库取值时的编码，不指定的话使用iso-8859-1。(只解决POST乱码)

            response.setContentType("text/html; charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            String userId = request.getParameter("userId");//请求数据
            String pwd = request.getParameter("pwd");

            if("admin".equals(userId) && "password".equals(pwd)){
                out.println("success");
            }else{
                out.println("fail");
            }
            out.println("<br/>");
            String servletPath = this.getServletContext().getRealPath("/");//路径?
            out.println(servletPath);
            out.close();

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}